package com.dianping.cat.home.service.transform;

import com.dianping.cat.home.service.IVisitor;
import com.dianping.cat.home.service.entity.Domain;
import com.dianping.cat.home.service.entity.ServiceReport;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  19 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  23 */     this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(ServiceReport serviceReport) {
/*  28 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  30 */     build(serviceReport, out);
    return out.toByteArray();
  }


/*  35 */   public static void build(ServiceReport serviceReport, OutputStream out) { serviceReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDomain(Domain domain) {
/*  40 */     if (domain.getId() != null) {
/*  41 */       writeTag(1, 1);
/*  42 */       writeString(domain.getId());
    }

/*  45 */     writeTag(2, 0);
/*  46 */     writeLong(domain.getTotalCount());

/*  48 */     writeTag(3, 0);
/*  49 */     writeLong(domain.getFailureCount());

/*  51 */     writeTag(4, 0);
/*  52 */     writeDouble(domain.getFailurePercent());

/*  54 */     writeTag(5, 0);
/*  55 */     writeDouble(domain.getSum());

/*  57 */     writeTag(6, 0);
/*  58 */     writeDouble(domain.getAvg());

/*  60 */     writeTag(7, 0);
/*  61 */     writeDouble(domain.getQps());

/*  63 */     writeTag(63, 3);
  }


  public void visitServiceReport(ServiceReport serviceReport) {
/*  68 */     writeTag(63, 0);

/*  70 */     if (serviceReport.getStartTime() != null) {
/*  71 */       writeTag(1, 1);
/*  72 */       writeDate(serviceReport.getStartTime());
    }

/*  75 */     if (serviceReport.getDomain() != null) {
/*  76 */       writeTag(2, 1);
/*  77 */       writeString(serviceReport.getDomain());
    }

/*  80 */     if (serviceReport.getEndTime() != null) {
/*  81 */       writeTag(3, 1);
/*  82 */       writeDate(serviceReport.getEndTime());
    }

/*  85 */     if (!serviceReport.getDomains().isEmpty()) {
/*  86 */       writeTag(33, 2);
/*  87 */       writeInt(serviceReport.getDomains().size());

/*  89 */       for (Domain domain : serviceReport.getDomains().values()) {
/*  90 */         domain.accept(this.m_visitor);
      }
    }

/*  94 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/*  99 */       writeVarint(value.getTime());
/* 100 */     } catch (Exception e) {
/* 101 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 107 */       writeVarint(Double.doubleToLongBits(value));
/* 108 */     } catch (Exception e) {
/* 109 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 115 */       writeVarint(value & 0xFFFFFFFFL);
/* 116 */     } catch (Exception e) {
/* 117 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 123 */       writeVarint(value);
/* 124 */     } catch (Exception e) {
/* 125 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 131 */       this.m_out.writeUTF(value);
/* 132 */     } catch (IOException e) {
/* 133 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 139 */       this.m_out.writeByte((field << 2) + type);
/* 140 */     } catch (IOException e) {
/* 141 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 147 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 148 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 151 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 152 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */